home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / icomplete.el.z / icomplete.el
Encoding:
Text File  |  1998-10-28  |  10.7 KB  |  288 lines

  1. ;;; icomplete.el --- minibuffer completion incremental feedback
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Ken Manheimer <klm@nist.gov>
  6. ;; Maintainer: Ken Manheimer <klm@nist.gov>
  7. ;; Created: Mar 1993 klm@nist.gov - first release to usenet
  8. ;; Keywords: help, abbrev
  9.  
  10. ;; This file is part of GNU Emacs.
  11.  
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;; GNU General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  24. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25. ;; Boston, MA 02111-1307, USA.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; Loading this package implements a more fine-grained minibuffer
  30. ;; completion feedback scheme.  Prospective completions are concisely
  31. ;; indicated within the minibuffer itself, with each successive
  32. ;; keystroke.
  33.  
  34. ;; See 'icomplete-completions' docstring for a description of the
  35. ;; icomplete display format.
  36.  
  37. ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
  38. ;; customize icomplete setup for interoperation with other
  39. ;; minibuffer-oriented packages.
  40.  
  41. ;; To activate icomplete mode, simply load the package.  You can
  42. ;; subsequently deactivate it by invoking the function icomplete-mode
  43. ;; with a negative prefix-arg (C-U -1 ESC-x icomplete-mode).  Also,
  44. ;; you can prevent activation of the mode during package load by
  45. ;; first setting the variable `icomplete-mode' to nil.  Icompletion
  46. ;; can be enabled any time after the package is loaded by invoking
  47. ;; icomplete-mode without a prefix arg.
  48.  
  49. ;; Thanks to everyone for their suggestions for refinements of this
  50. ;; package.  I particularly have to credit Michael Cook, who
  51. ;; implemented an incremental completion style in his 'iswitch'
  52. ;; functions that served as a model for icomplete.  Some other
  53. ;; contributors: Noah Freidman (restructuring as minor mode), Colin
  54. ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and
  55. ;; others.
  56.  
  57. ;; klm.
  58.  
  59. ;;; Code:
  60.  
  61. ;;;_* Provide
  62. (provide 'icomplete)
  63.  
  64. ;;;_* User Customization variables
  65.  
  66. ;;;_* Initialization
  67. ;;;_  = icomplete-minibuffer-setup-hook
  68. (defvar icomplete-minibuffer-setup-hook nil
  69.   "*Icomplete-specific customization of minibuffer setup.
  70.  
  71. This hook is run during minibuffer setup iff icomplete will be active.
  72. It is intended for use in customizing icomplete for interoperation
  73. with other packages.  For instance:
  74.  
  75.   \(add-hook 'icomplete-minibuffer-setup-hook 
  76.         \(function
  77.          \(lambda ()
  78.            \(make-local-variable 'resize-minibuffer-window-max-height)
  79.            \(setq resize-minibuffer-window-max-height 3))))
  80.  
  81. will constrain rsz-mini to a maximum minibuffer height of 3 lines when
  82. icompletion is occurring.")
  83.  
  84. ;;;_ + Internal Variables
  85. ;;;_  = icomplete-mode
  86. (defvar icomplete-mode t
  87.   "Non-nil enables incremental minibuffer completion, once
  88. `\\[icomplete-mode]' function has set things up.")
  89. ;;;_  = icomplete-eoinput 1
  90. (defvar icomplete-eoinput 1
  91.   "Point where minibuffer input ends and completion info begins.")
  92. (make-variable-buffer-local 'icomplete-eoinput)
  93. ;;;_  = icomplete-pre-command-hook
  94. (defvar icomplete-pre-command-hook nil
  95.   "Incremental-minibuffer-completion pre-command-hook.
  96.  
  97. Is run in minibuffer before user input when `icomplete-mode' is non-nil.
  98. Use `icomplete-mode' function to set it up properly for incremental
  99. minibuffer completion.")
  100. (add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
  101. ;;;_  = icomplete-post-command-hook
  102. (defvar icomplete-post-command-hook nil
  103.   "Incremental-minibuffer-completion post-command-hook.
  104.  
  105. Is run in minibuffer after user input when `icomplete-mode' is non-nil.
  106. Use `icomplete-mode' function to set it up properly for incremental
  107. minibuffer completion.")
  108. (add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
  109.  
  110. ;;;_ > icomplete-mode (&optional prefix)
  111. ;;;###autoload
  112. (defun icomplete-mode (&optional prefix)
  113.   "Activate incremental minibuffer completion for this emacs session,
  114. or deactivate with negative prefix arg."
  115.   (interactive "p")
  116.   (or prefix (setq prefix 0))
  117.   (cond ((>= prefix 0)
  118.      (setq icomplete-mode t)
  119.      ;; The following is not really necessary after first time -
  120.      ;; no great loss.
  121.      (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup))
  122.     (t (setq icomplete-mode nil))))
  123.  
  124. ;;;_ > icomplete-simple-completing-p ()
  125. (defun icomplete-simple-completing-p ()
  126.  
  127.   "Non-nil if current window is minibuffer that's doing simple completion.
  128.  
  129. Conditions are:
  130.    the selected window is a minibuffer,
  131.    and not in the middle of macro execution,
  132.    and minibuffer-completion-table is not a symbol (which would
  133.        indicate some non-standard, non-simple completion mechanism,
  134.        like file-name and other custom-func completions)."
  135.  
  136.   (and (window-minibuffer-p (selected-window))
  137.        (not executing-kbd-macro)
  138.        (not (symbolp minibuffer-completion-table))))
  139.  
  140. ;;;_ > icomplete-minibuffer-setup ()
  141. ;;;###autoload
  142. (defun icomplete-minibuffer-setup ()
  143.   "Run in minibuffer on activation to establish incremental completion.
  144. Usually run by inclusion in `minibuffer-setup-hook'."
  145.   (cond ((and icomplete-mode (icomplete-simple-completing-p))
  146.      (make-local-hook 'pre-command-hook)
  147.      (add-hook 'pre-command-hook
  148.            (function (lambda ()
  149.                    (run-hooks 'icomplete-pre-command-hook)))
  150.            nil t)
  151.      (make-local-hook 'post-command-hook)
  152.      (add-hook 'post-command-hook
  153.            (function (lambda ()
  154.                    (run-hooks 'icomplete-post-command-hook)))
  155.            nil t)
  156.      (run-hooks 'icomplete-minibuffer-setup-hook))))
  157.  
  158. ;;;_* Completion
  159.  
  160. ;;;_ > icomplete-tidy ()
  161. (defun icomplete-tidy ()
  162.   "Remove completions display \(if any) prior to new user input.
  163. Should be run in on the minibuffer `pre-command-hook'.  See `icomplete-mode'
  164. and `minibuffer-setup-hook'."
  165.   (if (icomplete-simple-completing-p)
  166.       (if (and (boundp 'icomplete-eoinput)
  167.            icomplete-eoinput)
  168.  
  169.       (if (> icomplete-eoinput (point-max))
  170.           ;; Oops, got rug pulled out from under us - reinit:
  171.           (setq icomplete-eoinput (point-max))
  172.         (let ((buffer-undo-list buffer-undo-list )) ; prevent entry
  173.           (delete-region icomplete-eoinput (point-max))))
  174.  
  175.     ;; Reestablish the local variable 'cause minibuffer-setup is weird:
  176.     (make-local-variable 'icomplete-eoinput)
  177.     (setq icomplete-eoinput 1))))
  178.  
  179. ;;;_ > icomplete-exhibit ()
  180. (defun icomplete-exhibit ()
  181.   "Insert icomplete completions display.
  182. Should be run via minibuffer `post-command-hook'.  See `icomplete-mode'
  183. and `minibuffer-setup-hook'."
  184.   (if (icomplete-simple-completing-p)
  185.       (let ((contents (buffer-substring (point-min)(point-max)))
  186.         (buffer-undo-list t))
  187.     (save-excursion
  188.       (goto-char (point-max))
  189.                                         ; Register the end of input, so we
  190.                                         ; know where the extra stuff
  191.                                         ; (match-status info) begins:
  192.       (if (not (boundp 'icomplete-eoinput))
  193.           ;; In case it got wiped out by major mode business:
  194.           (make-local-variable 'icomplete-eoinput))
  195.       (setq icomplete-eoinput (point))
  196.                                         ; Insert the match-status information:
  197.       (if (> (point-max) 1)
  198.           (insert-string
  199.            (icomplete-completions contents
  200.                       minibuffer-completion-table
  201.                       minibuffer-completion-predicate
  202.                       (not
  203.                        minibuffer-completion-confirm))))))))
  204.  
  205. ;;;_ > icomplete-completions (name candidates predicate require-match)
  206. (defun icomplete-completions (name candidates predicate require-match)
  207.   "Identify prospective candidates for minibuffer completion.
  208.  
  209. The display is updated with each minibuffer keystroke during
  210. minibuffer completion.
  211.  
  212. Prospective completion suffixes (if any) are displayed, bracketed by
  213. one of \(), \[], or \{} pairs.  The choice of brackets is as follows:
  214.  
  215.   \(...) - a single prospect is identified and matching is enforced,
  216.   \[...] - a single prospect is identified but matching is optional, or
  217.   \{...} - multiple prospects, separated by commas, are indicated, and
  218.           further input is required to distinguish a single one.
  219.  
  220. The displays for unambiguous matches have ` [Matched]' appended
  221. \(whether complete or not), or ` \[No matches]', if no eligible
  222. matches exist."
  223.  
  224.   (let ((comps (all-completions name candidates predicate))
  225.                                         ; "-determined" - only one candidate
  226.         (open-bracket-determined (if require-match "(" "["))
  227.         (close-bracket-determined (if require-match ")" "]"))
  228.                                         ;"-prospects" - more than one candidate
  229.         (open-bracket-prospects "{")
  230.         (close-bracket-prospects "}")
  231.         )
  232.     (cond ((null comps) (format " %sNo matches%s"
  233.                                 open-bracket-determined
  234.                                 close-bracket-determined))
  235.           ((null (cdr comps))           ;one match
  236.            (concat (if (and (> (length (car comps))
  237.                                (length name)))
  238.                        (concat open-bracket-determined
  239.                                (substring (car comps) (length name))
  240.                                close-bracket-determined)
  241.                      "")
  242.                    " [Matched]"))
  243.           (t                            ;multiple matches
  244.            (let* ((most (try-completion name candidates predicate))
  245.                   (most-len (length most))
  246.                   most-is-exact
  247.                   (alternatives
  248.                    (apply
  249.                     (function concat)
  250.                     (cdr (apply
  251.               (function nconc)
  252.               (mapcar '(lambda (com)
  253.                      (if (= (length com) most-len)
  254.                      ;; Most is one exact match,
  255.                      ;; note that and leave out
  256.                      ;; for later indication:
  257.                      (progn
  258.                        (setq most-is-exact t)
  259.                        ())
  260.                        (list ","
  261.                          (substring com
  262.                             most-len))))
  263.                   comps))))))
  264.              (concat (and (> most-len (length name))
  265.                           (concat open-bracket-determined
  266.                                   (substring most (length name))
  267.                                   close-bracket-determined))
  268.                      open-bracket-prospects
  269.                      (if most-is-exact
  270.                          (concat "," alternatives)
  271.                        alternatives)
  272.                      close-bracket-prospects))))))
  273.  
  274. ;;;_ + Initialization
  275. ;;; If user hasn't setq-default icomplete-mode to nil, then setup for
  276. ;;; activation:
  277. (if icomplete-mode
  278.     (icomplete-mode))
  279.  
  280.  
  281. ;;;_* Local emacs vars.
  282. ;;;Local variables:
  283. ;;;outline-layout: (-2 :)
  284. ;;;End:
  285.  
  286. ;;; icomplete.el ends here
  287.  
  288.